package com.health.yb.web.bean.common;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

/**
 *
 * @author FeiLiu
 * @date 2018/1/2
 */
@JsonIgnoreProperties({ "logMsg"})//转换为Json数据时忽略logMsg属性.
public class Response<T> implements Serializable {

    private static final long serialVersionUID = -2961652590351336247L;
    private String OK = "ok";
    private String ERROR = "error";
    //日志信息.
    @ApiModelProperty(value = "日志对象",hidden = true)
    private StringBuilder logMsg;
    @ApiModelProperty(value = "返回消息对象")
    private Meta meta;

    private T data;

    public Response(){
        this.meta = new Meta();
    }

    public Response success() {
        this.meta = new Meta(true, null, OK);
        return this;
    }

    public Response successWithMsg(String msg) {
        this.meta = new Meta(true, null, msg);
        return this;
    }

    public Response append(String logInfo) {
    	if(logMsg == null) {
    		logMsg = new StringBuilder();
    	}
    	logMsg.append(logInfo);
    	return this;
    }
    
    public StringBuilder getLogMsg() {
    	return logMsg;
    }
    
    public Response setLogMsg(StringBuilder logMsg) {
		this.logMsg = logMsg;
		return this;
	}

	public Response successWithData(T data) {
        this.meta = new Meta(true, null, OK);
        this.data = data;
        return this;
    }

    public Response successWithData(T data, String msg) {
        this.meta = new Meta(true, null, msg);
        this.data = data;
        return this;
    }

    public Response failure() {
        this.meta = new Meta(false, null, ERROR);
        return this;
    }

    public Response failureWithCode(Integer errorCode) {
        this.meta = new Meta(false, errorCode, "服务端错误");
        return this;
    }

    public Response failureWithMsg(String msg) {
        this.meta = new Meta(false, 500, msg);
        return this;
    }

    public Meta getMeta() {
        return meta;
    }

    public T getData() {
        return data;
    }

    public class Meta implements Serializable{
    	
        /**
		 * 
		 */
		private static final long serialVersionUID = 2279850964625965694L;
        @ApiModelProperty(value = "是否成功",notes = "true or false ")
        private boolean success;
        @ApiModelProperty(value = "返回消息",notes = "success对应的消息信息")
        private String message;
        @ApiModelProperty(value = "错误code",notes = "为false时，对应的错误code")
        private Integer errorCode;

        public Meta(){}

        public Meta(boolean success, Integer errorCode, String message) {
            this.success = success;
            this.message = message;
            this.errorCode = errorCode;
        }

        public Meta(boolean success) {
            this.success = success;
        }

        public boolean isSuccess() {
            return success;
        }

        public String getMessage() {
            return message;
        }

        public Integer getErrorCode() {
            return errorCode;
        }

        public void setErrorCode(Integer errorCode) {
            this.errorCode = errorCode;
        }
    }
}
